/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf;

/**
 *
 * @author Luis Molinari
 */
import javax.faces.bean.ManagedBean;
import javax.faces.bean.*;
import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import org.primefaces.event.*;
import org.primefaces.model.*;
import util.*;

@ManagedBean
@ViewScoped
public class AgendaController implements Serializable {

    private Locale ptBr = new Locale("pt", "BR");
    private ScheduleModel eventModel;
    private ScheduleModel lazyEventModel;
    private ScheduleEvent event = new ClienteScheduleEvent();

    public AgendaController() {
        //getEventModel().addEvent(new ClienteScheduleEvent("Teste", new Date(), new Date(), "service"));
    }

    public void addEvent(ActionEvent actionEvent) {
        InsertUpdateEvent();

        event = new ClienteScheduleEvent();
    }

    public void onEventSelect(ScheduleEntrySelectEvent selectEvent) {
        event = selectEvent.getScheduleEvent();
    }

    public void onDateSelect(DateSelectEvent selectEvent) {
        Calendar finishDate = Calendar.getInstance();
        finishDate.setTime(selectEvent.getDate());
        finishDate.add(Calendar.HOUR, 1);
        event = new ClienteScheduleEvent("", selectEvent.getDate(), finishDate.getTime(), "");
    }

    public void onEventMove(ScheduleEntryMoveEvent event) {
        this.event = event.getScheduleEvent();
        InsertUpdateEvent();

        this.event = new ClienteScheduleEvent();
    }

    public void onEventResize(ScheduleEntryResizeEvent event) {
        this.event = event.getScheduleEvent();
        InsertUpdateEvent();

        this.event = new ClienteScheduleEvent();
    }

    public Locale getPtBr() {
        return ptBr;
    }

    public ScheduleModel getEventModel() {
        if (eventModel == null) {
            eventModel = new DefaultScheduleModel();
        }
        return eventModel;
    }

    public ScheduleModel getLazyEventModel() {
        return lazyEventModel;
    }

    public ScheduleEvent getEvent() {
        return event;
    }

    private void InsertUpdateEvent() {
        if (getEvent().getId() == null) {
            getEventModel().addEvent(getEvent());
        } else {
            getEventModel().updateEvent(getEvent());
        }
    }
}
